package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.domain.*;
import com.ruoyi.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.IZhwyTemporarydepositService;

/**
 * 临时收费/押金管理Service业务层处理
 * 
 * @author liuzhiqiang
 * @date 2024-05-07
 */
@Service
public class ZhwyTemporarydepositServiceImpl implements IZhwyTemporarydepositService 
{
    @Autowired
    private ZhwyTemporarydepositMapper zhwyTemporarydepositMapper;
    @Autowired
    private ZhwyRentalcontractMapper zhwyRentalcontractMapper;
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;
    @Autowired
    private ZhwyHirerMapper zhwyHirerMapper;
    @Autowired
    private ZhwyChargeitemMapper zhwyChargeitemMapper;
    @Autowired
    private ZhwyPaymentmethodMapper zhwyPaymentmethodMapper;
    @Autowired
    private ZhwyBuildingMapper zhwyBuildingMapper;
    @Autowired
    private ZhwyDowntownMapper zhwyDowntownMapper;

    /**
     * 查询临时收费/押金管理
     * 
     * @param id 临时收费/押金管理主键
     * @return 临时收费/押金管理
     */
    @Override
    public ZhwyTemporarydeposit selectZhwyTemporarydepositById(Long id)
    {
        return zhwyTemporarydepositMapper.selectZhwyTemporarydepositById(id);
    }

    /**
     * 查询临时收费/押金管理列表
     * 
     * @param zhwyTemporarydeposit 临时收费/押金管理
     * @return 临时收费
     */
    @Override
    public List<ZhwyTemporarydeposit> selectZhwyTemporarydepositList(ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        List<ZhwyTemporarydeposit> temporarydepositList = zhwyTemporarydepositMapper.selectZhwyTemporarydepositList(zhwyTemporarydeposit);
        // 过滤掉 state 属性为 2 或 3 的对象
        List<ZhwyTemporarydeposit> filteredList = temporarydepositList.stream()
                .filter(item -> item.getState() != 2 && item.getState() != 3)
                .collect(Collectors.toList());
        for(ZhwyTemporarydeposit zh:filteredList){

            //获取关联收款项目
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(zh.getChargeitemId());
            zh.setChargeitemName(zhwyChargeitem.getName());
            //获取支付方式
            ZhwyPaymentmethod zhwyPaymentmethod = zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zh.getPayId());
            zh.setPayName(zhwyPaymentmethod.getName());
            //获取退款方式
            if(zh.getRefundId()!=null){
                ZhwyPaymentmethod zhwyPaymentmethod1 = zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zh.getRefundId());
                zh.setRefunName(zhwyPaymentmethod1.getName());
            }

            if(zh.getShopsId()!=null){
                //获取关联商铺
                ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zh.getShopsId());
                zh.setShopName(zhwyShops.getCode());
                //获取客户名字
                List<ZhwyRentalcontract> zhwyRentalcontracts = zhwyRentalcontractMapper.selectZhwyRentalcontractByshopId(zh.getShopsId());

                for(ZhwyRentalcontract ren:zhwyRentalcontracts){
                    ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(ren.getHirerId());
                    zh.setName(zhwyHirer.getName());
                }

            }
        }
        return filteredList;
    }
    ///押金管理
    public List<ZhwyTemporarydeposit> selectZhwyTemporarydepositList1(ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        List<ZhwyTemporarydeposit> temporarydepositList = zhwyTemporarydepositMapper.selectZhwyTemporarydepositList(zhwyTemporarydeposit);
        // 过滤掉 state 属性为 0 或 1 的对象
        List<ZhwyTemporarydeposit> filteredList = temporarydepositList.stream()
                .filter(item -> item.getState() != 0 && item.getState() != 1)
                .collect(Collectors.toList());
        for(ZhwyTemporarydeposit zh:filteredList){

            //获取关联收款项目
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(zh.getChargeitemId());
            zh.setChargeitemName(zhwyChargeitem.getName());
            //获取支付方式
            ZhwyPaymentmethod zhwyPaymentmethod = zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zh.getPayId());
            zh.setPayName(zhwyPaymentmethod.getName());
            //获取退款方式
            if(zh.getRefundId()!=null){
                ZhwyPaymentmethod zhwyPaymentmethod1 = zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zh.getRefundId());
                zh.setRefunName(zhwyPaymentmethod1.getName());
            }

            if(zh.getShopsId()!=null){
                //获取关联商铺
                ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zh.getShopsId());
                zh.setShopName(zhwyShops.getCode());
                //获取客户名字
                List<ZhwyRentalcontract> zhwyRentalcontracts = zhwyRentalcontractMapper.selectZhwyRentalcontractByshopId(zh.getShopsId());
                for(ZhwyRentalcontract ren:zhwyRentalcontracts){
                    ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(ren.getHirerId());
                    zh.setName(zhwyHirer.getName());
                }

            }
        }
        return filteredList;
    }

    /**
     * 新增临时收费/押金管理
     * 
     * @param zhwyTemporarydeposit 临时收费/押金管理
     * @return 结果
     */
    @Override
    public int insertZhwyTemporarydeposit(ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        return zhwyTemporarydepositMapper.insertZhwyTemporarydeposit(zhwyTemporarydeposit);
    }

    /**
     * 修改临时收费/押金管理
     * 
     * @param zhwyTemporarydeposit 临时收费/押金管理
     * @return 结果
     */
    @Override
    public int updateZhwyTemporarydeposit(ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        return zhwyTemporarydepositMapper.updateZhwyTemporarydeposit(zhwyTemporarydeposit);
    }

    /**
     * 批量删除临时收费/押金管理
     * 
     * @param ids 需要删除的临时收费/押金管理主键
     * @return 结果
     */
    @Override
    public int deleteZhwyTemporarydepositByIds(Long[] ids)
    {
        return zhwyTemporarydepositMapper.deleteZhwyTemporarydepositByIds(ids);
    }

    /**
     * 删除临时收费/押金管理信息
     * 
     * @param id 临时收费/押金管理主键
     * @return 结果
     */
    @Override
    public int deleteZhwyTemporarydepositById(Long id)
    {
        return zhwyTemporarydepositMapper.deleteZhwyTemporarydepositById(id);
    }

    @Override
    public List<ZhwyTemporarydeposit> selectByTime(Date time) {
        List list = new ArrayList<>();
        List<ZhwyTemporarydeposit> tdList = zhwyTemporarydepositMapper.selectByTime(time);
        List<ZhwyChargeitem> zcList = zhwyChargeitemMapper.selectByType();
        for (ZhwyChargeitem zc : zcList) {
            list.add(zc.getId());
        }
        for (ZhwyTemporarydeposit zt : tdList) {
            if(!list.contains(zt.getChargeitemId())){
                tdList.remove(zt);
            }
        }
        for (ZhwyTemporarydeposit ztd : tdList) {
            ZhwyChargeitem zc = zhwyChargeitemMapper.selectZhwyChargeitemById(ztd.getChargeitemId());
            ztd.setcName(zc.getName());
        }
        return tdList;
    }

    @Override
    public List<ZhwyTemporarydeposit> selectByTime1(Date time) {
        List list = new ArrayList<>();
        List<ZhwyTemporarydeposit> tdList = zhwyTemporarydepositMapper.selectByTime(time);
        System.out.println("///////////"+tdList.toString());
        List<ZhwyChargeitem> zcList = zhwyChargeitemMapper.selectByType1();
        System.out.println("============="+zcList.toString());
        for (ZhwyChargeitem zc : zcList) {
            list.add(zc.getId());
        }
        System.out.println("-=-=-=-=-="+list.toString());
        for (ZhwyTemporarydeposit zt : tdList) {
            if(!list.contains(zt.getChargeitemId())){
                tdList.remove(zt);
            }
            System.out.println("=-=-=-=-=-"+tdList.toString());
            if (tdList.size()==0){
                break;
            }
        }

        if(tdList.size()!=0){
            for (ZhwyTemporarydeposit ztd : tdList) {
                ZhwyChargeitem zc = zhwyChargeitemMapper.selectZhwyChargeitemById(ztd.getChargeitemId());
                ztd.setcName(zc.getName());
            }
            return tdList;
        }

       return tdList;

    }

    @Override
    public List<ZhwyTemporarydeposit> selectByTime2(Date time) {
        List list = new ArrayList<>();
        List<ZhwyTemporarydeposit> tdList = zhwyTemporarydepositMapper.selectByTime(time);
        List<ZhwyChargeitem> zcList = zhwyChargeitemMapper.selectByType2();
        for (ZhwyChargeitem zc : zcList) {
            list.add(zc.getId());
        }
        for (ZhwyTemporarydeposit zt : tdList) {
            if(!list.contains(zt.getChargeitemId())){
                tdList.remove(zt);
            }
            if (tdList.size()==0){
                break;
            }
        }
        if(tdList.size()!=0){
        for (ZhwyTemporarydeposit ztd : tdList) {
            ZhwyChargeitem zc = zhwyChargeitemMapper.selectZhwyChargeitemById(ztd.getChargeitemId());
            ztd.setcName(zc.getName());
        }
        return tdList;}
        return tdList;
    }
}
